﻿import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Controls.Basic
import FilUI

Button {
    id: control

    property size iconSizeUrl            : Qt.size(-1, -1)
    property url iconSourceUrl           : ""
    property int iconSize                : 20
    property int iconSource              : 0
    property bool disable                : false
    property int radius                  : 4
    property string contentDescription   : ""
    property color hoverColor            : FilTheme.itemHoverColor
    property color pressedColor          : FilTheme.itemPressColor
    property color normalColor           : FilTheme.itemNormalColor
    property color disableColor          : FilTheme.itemNormalColor
    property Component iconDelegate      : 0 < iconSource ? com_icon : com_icon_url
    property color color                 : {
        if (!enabled) {
            return disableColor
        }
        if (pressed) {
            return pressedColor
        }
        return hovered ? hoverColor : normalColor
    }
    property color iconColor             : {
        if (!enabled) {
            return Qt.rgba(161/255, 161/255, 161/255, 1)
        }
        return Qt.rgba(0,0,0,1)
    }
    property color textColor             : {
        if (!enabled) {
            return Qt.rgba(161/255, 161/255, 161/255, 1)
        }
        return Qt.rgba(0,0,0,1)
    }

    display                              : Button.IconOnly
    Accessible.role                      : Accessible.Button
    Accessible.name                      : control.text
    Accessible.description               : contentDescription
    Accessible.onPressAction             : control.clicked()
    focusPolicy                          : Qt.TabFocus
    padding                              : 0
    verticalPadding                      : 8
    horizontalPadding                    : 8
    enabled                              : !disable
    font                                 : FilTextStyle.Caption
    background                           : Rectangle {
        implicitWidth                    : 30
        implicitHeight                   : 30
        radius                           : control.radius
        color                            : control.color
        FilFocusRectangle {
            visible                      : control.activeFocus
        }
    }

    Component {
        id: com_icon
        FilIcon {
            id: text_icon
            font.pixelSize               : iconSize
            iconSize                     : control.iconSize
            horizontalAlignment          : Text.AlignHCenter
            iconColor                    : control.iconColor
            iconSource                   : control.iconSource
        }

    }

    Component {
        id: com_icon_url
        Image {
            id: id_image__icon
            source                       : iconSourceUrl
            sourceSize                   : control.iconSizeUrl
            horizontalAlignment          : Text.AlignHCenter
            verticalAlignment            : Text.AlignVCenter
        }
    }

    Component {
        id: com_row
        RowLayout {
            Loader {
                sourceComponent : iconDelegate
                Layout.alignment: Qt.AlignVCenter | Qt.AlignHCenter
                visible         : display !== Button.TextOnly
            }
            Text {
                text            : control.text
                Layout.alignment: Qt.AlignVCenter | Qt.AlignHCenter
                visible         : display !== Button.IconOnly
                color           : control.textColor
                font            : control.font
            }
        }
    }

    Component {
        id: com_column
        ColumnLayout {
            Loader {
                sourceComponent : iconDelegate
                Layout.alignment: Qt.AlignVCenter | Qt.AlignHCenter
                visible         : display !== Button.TextOnly
            }
            Text {
                text            : control.text
                Layout.alignment: Qt.AlignVCenter | Qt.AlignHCenter
                visible         : display !== Button.IconOnly
                color           : control.textColor
                font            : control.font
            }
        }
    }

    contentItem                           : Loader {
        sourceComponent         : display === Button.TextUnderIcon ? com_column : com_row
    }

    ToolTip {
        id: id_tooltip
        text                    : control.text
        delay                   : 1000
        visible                 : {
            if (control.text === "") {
                return false
            }
            if (control.display !== Button.IconOnly) {
                return false
            }
            return hovered
        }
    }
}
